// MouseMage, a rodent behavioral data collection and analysis program for Android devices.
// Copyright (C) 2012 Joseph Van Swearingen
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package com.zythran.mousemage;

import android.util.Log;

public class MouseResult {

	public int protocol ;
	
	public MouseResult() {
		
	}
	
	public void set_protocol(int i_protocol) {
		protocol = i_protocol ;
	}
	
	public void generateResult(MouseIteration mi) {

		if (protocol == 0) {
			
			generate_rat_result(mi) ;
			
		} else if (protocol == 1) {
			
			generate_mouse_result(mi) ;
			
		}
	}
	
	protected void generate_rat_result(MouseIteration mi) {
		
		int final_result = 0 ;
		
		if ( (mi.move_level == 0) &&
			 (mi.sniff_intense ||
			  mi.groom_intense ||
			  mi.head_bob)) {
			
			final_result = 6 ;
			
		} else if ((mi.move_level >= mi.MOVE_LEVEL_INTERMITTENT) &&
				(mi.sniff_intense ||
				 mi.groom_intense ||
				 mi.head_bob ||
				 mi.movement_pattern)) {
			
			final_result = 5 ;
			
		} else if ((mi.sniff_level >= mi.SNIFF_LEVEL_CONTINUOUS) &&
				(mi.move_level >= mi.MOVE_LEVEL_CONTINUOUS)) {
			
			final_result = 4 ;
			
		} else if ((mi.sniff_level >= mi.SNIFF_LEVEL_INTERMITTENT) &&
				(mi.move_level == mi.MOVE_LEVEL_INTERMITTENT ||
				 mi.rear_level == mi.REAR_LEVEL_INTERMITTENT ||
				 mi.rear_level == mi.REAR_LEVEL_CONTINUOUS)) {
			
			final_result = 3 ;
			
		} else if (!mi.sniff_intense &&
				   !mi.groom_intense &&
				   !mi.movement_pattern &&
				   ( mi.sniff_level == mi.SNIFF_LEVEL_CONTINUOUS ||
				     mi.sniff_level == mi.SNIFF_LEVEL_INTERMITTENT ||
				     mi.groom_level == mi.GROOM_LEVEL_INTERMITTENT ||
				     mi.groom_level == mi.GROOM_LEVEL_CONTINUOUS ||
				     mi.rear_level == mi.REAR_LEVEL_INTERMITTENT ||
				     mi.rear_level == mi.REAR_LEVEL_CONTINUOUS ||
				     mi.move_level == mi.MOVE_LEVEL_INTERMITTENT ||
				     mi.move_level == mi.MOVE_LEVEL_CONTINUOUS)) {
			
			final_result = 2 ;
			
		} else if (mi.asleep_marked ||
				   mi.inactive_marked ||
				    (mi.sniff_level == mi.SNIFF_LEVEL_ONCE)) {
			
			final_result = 1 ;
			
		}
		
		mi.final_result = final_result ;
		Log.i("protocol", "protocol 0") ;
	}
	
	protected void generate_mouse_result(MouseIteration mi) {
		
		int final_result ;
		
		// highest score is now a 9
	    if (mi.limbs_splayed || mi.seizures || mi.laying_on_side) {
	    	
	        final_result = 9;
	        
	    } else if (mi.rear_level == mi.REAR_LEVEL_MAINTAINED) {
	    	
	        final_result = 8;
	        
	    } else if (mi.rear_level == mi.REAR_LEVEL_CONTINUOUS) {
	    	
	        final_result = 7;

	    } else if (mi.movement_pattern && (mi.move_level >= mi.MOVE_LEVEL_INTERMITTENT)) {
	    	
	        final_result = 6;

        // 1. grooming+intense
        // 2. sniffing+intense (once/twice and intense are mutually exclusive)
        // 3. sniffing>=intermittent (other) head bob/weave (once-twice and head bob/weave are mutually exclusive)
        // 4. pivot >= intermittent
	    } else if (
	    	((mi.groom_level >= mi.GROOM_LEVEL_INTERMITTENT) && mi.groom_intense) ||
	    	((mi.sniff_level >= mi.SNIFF_LEVEL_INTERMITTENT) && mi.sniff_intense) ||
	    	((mi.sniff_level >= mi.SNIFF_LEVEL_INTERMITTENT) && mi.head_bob) ||
	    	(mi.pivot_level >= mi.PIVOT_LEVEL_INTERMITTENT)) {
	    	
            final_result = 5;

        // 1. locomotion>=intermittent +fast+exploratory
	    } else if (
	    	((mi.move_level >= mi.MOVE_LEVEL_INTERMITTENT) && mi.move_fast)) {
	    	
            final_result = 4;

        // 1. locomotion==intermittent+exploratory-patterned
        // 2. (other) eating bedding
        // 3. rearing=intermittent
	    } else if (
	    	((mi.move_level == mi.MOVE_LEVEL_INTERMITTENT) && mi.exploring) ||
	    	(mi.eating_bedding) ||
	    	(mi.rear_level == mi.REAR_LEVEL_INTERMITTENT)
	    	) {
	    	
            final_result = 3;

        // 1. sniffing==intermittent -intense -head_bob/weave
        // 2. sniffing==continuous -intense -head_bob/weave
        // 3. grooming==intermittent -intense
        // 4. grooming==continuous -intense
        // 5. pivot==once
	    } else if (
	    	((mi.sniff_level == mi.SNIFF_LEVEL_INTERMITTENT) && !mi.sniff_intense && !mi.head_bob) ||
	    	((mi.sniff_level == mi.SNIFF_LEVEL_CONTINUOUS) && !mi.sniff_intense && !mi.head_bob) ||
	    	((mi.groom_level == mi.GROOM_LEVEL_INTERMITTENT) && !mi.groom_intense) ||
	    	((mi.groom_level == mi.GROOM_LEVEL_CONTINUOUS) && !mi.groom_intense) ||
	    	(mi.pivot_level == mi.PIVOT_LEVEL_ONCE)) {
	    	
    	    final_result = 2;

        // (asleep || inactive) && (sniffing <= once/twice)
    	} else if ((mi.asleep_marked || mi.inactive_marked) && (mi.sniff_level <= 1)) {
    		
    	    final_result = 1;

    	} else {
    	    final_result = 0; // error
    	}

		mi.final_result = final_result;
		Log.i("protocol", "protocol 1") ;
	}

}
